package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TraceabilityMapper;
import com.ruoyi.system.domain.Traceability;
import com.ruoyi.system.service.ITraceabilityService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-30
 */
@Service
public class TraceabilityServiceImpl implements ITraceabilityService 
{
    @Autowired
    private TraceabilityMapper traceabilityMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Traceability selectTraceabilityById(Long id)
    {
        return traceabilityMapper.selectTraceabilityById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param traceability 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Traceability> selectTraceabilityList(Traceability traceability)
    {
        return traceabilityMapper.selectTraceabilityList(traceability);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param traceability 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTraceability(Traceability traceability)
    {
        return traceabilityMapper.insertTraceability(traceability);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param traceability 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTraceability(Traceability traceability)
    {
        return traceabilityMapper.updateTraceability(traceability);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTraceabilityByIds(Long[] ids)
    {
        return traceabilityMapper.deleteTraceabilityByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTraceabilityById(Long id)
    {
        return traceabilityMapper.deleteTraceabilityById(id);
    }

    @Override
    public List<Traceability> selectTraceabilityListGroup(String taskId) {
       List<Traceability> yList = traceabilityMapper.selectTraceabilityListBytaskIdY(taskId);
        List<Traceability> nList = traceabilityMapper.selectTraceabilityListBytaskIdN(taskId);
        List<String> textList = traceabilityMapper.selectTestGroupBytaskId(taskId);

        // 创建一个Map对象，用于存储分组后的数据
        List<Traceability> dataList = new ArrayList<>();
        for (String text : textList) {
            Traceability trac1y= new Traceability();
            Traceability trac2y= new Traceability();
            Traceability trac3y= new Traceability();
            Traceability trac1n= new Traceability();
            Traceability trac2n= new Traceability();
            Traceability trac3n= new Traceability();
            trac1y.setTestId(text);
            trac1y.setStartNum(0L);
            trac1y.setEndNum(7L);
            trac2y.setTestId(text);
            trac2y.setStartNum(8L);
            trac2y.setEndNum(15L);
            trac3y.setTestId(text);
            trac3y.setStartNum(16L);
            trac3y.setEndNum(23L);
            trac1n.setTestId(text);
            trac1n.setStartNum(0L);
            trac1n.setEndNum(7L);
            trac2n.setTestId(text);
            trac2n.setStartNum(8L);
            trac2n.setEndNum(15L);
            trac3n.setTestId(text);
            trac3n.setStartNum(16L);
            trac3n.setEndNum(23L);
            // 查询每个分组的数据
            BigDecimal dueTimes1 = traceabilityMapper.selectDueTimesByTaskId(trac1y);
            BigDecimal dueTimes2 = traceabilityMapper.selectDueTimesByTaskId(trac2y);
            BigDecimal dueTimes3 = traceabilityMapper.selectDueTimesByTaskId(trac3y);
            BigDecimal revTimes1 = traceabilityMapper.selectRevTimesByTaskId(trac1n);
            BigDecimal revTimes2 = traceabilityMapper.selectRevTimesByTaskId(trac2n);
            BigDecimal revTimes3 = traceabilityMapper.selectRevTimesByTaskId(trac3n);
            // 创建一个List对象，用于存储每个分组的数据
            for (Traceability traceability : yList) {
                //序号位于1-8、9-16、17-24分别为1、2、3
                if (traceability.getNumber() >= 1 && traceability.getNumber() <= 8) {
                    traceability.setFrequency(1L);
                    traceability.setStartNum(1L);
                    traceability.setEndNum(8L);
                    if (text.equals(traceability.getTestId())) {
                        BigDecimal result = new BigDecimal(30).divide(dueTimes1, 4, RoundingMode.HALF_UP);
                        traceability.setAbsSpeed(result);
                        traceability.setAbsTime(dueTimes1);
                    }

                } else if (traceability.getNumber() >= 9 && traceability.getNumber() <= 16) {
                    traceability.setFrequency(2L);
                    traceability.setStartNum(9L);
                    traceability.setEndNum(16L);
                    if (text.equals(traceability.getTestId())) {
                    traceability.setAbsTime(dueTimes2);
                    BigDecimal result = new BigDecimal(30).divide(dueTimes2, 3, RoundingMode.HALF_UP);
                    traceability.setAbsSpeed(result);}
                } else if (traceability.getNumber() >= 17 && traceability.getNumber() <= 24) {
                    traceability.setFrequency(3L);
                    traceability.setStartNum(17L);
                    traceability.setEndNum(24L);
                    if (text.equals(traceability.getTestId())) {
                    traceability.setAbsTime(dueTimes3);
                    BigDecimal result = new BigDecimal(30).divide(dueTimes3, 3, RoundingMode.HALF_UP);
                    traceability.setAbsSpeed(result);}
                }

                if (text.equals(traceability.getTestId())) {
                    dataList.add(traceability);
                }
            }
            for (Traceability traceability : nList) {
                if (traceability.getNumber() >= 1 && traceability.getNumber() <= 8) {
                    traceability.setFrequency(1L);
                    traceability.setStartNum(1L);
                    traceability.setEndNum(8L);
                    if (text.equals(traceability.getTestId())) {
                    traceability.setAbsTime(revTimes1);
                    BigDecimal result = new BigDecimal(30).divide(revTimes1, 3, RoundingMode.HALF_UP);
                    traceability.setAbsSpeed(result);}
                } else if (traceability.getNumber() >= 9 && traceability.getNumber() <= 16) {
                    traceability.setFrequency(2L);
                    traceability.setStartNum(9L);
                    traceability.setEndNum(16L);
                    if (text.equals(traceability.getTestId())) {
                    traceability.setAbsTime(revTimes2);
                    BigDecimal result = new BigDecimal(30).divide(revTimes2, 3, RoundingMode.HALF_UP);
                    traceability.setAbsSpeed(result);}
                } else if (traceability.getNumber() >= 17 && traceability.getNumber() <= 24) {
                    traceability.setFrequency(3L);
                    traceability.setStartNum(17L);
                    traceability.setEndNum(24L);
                    if (text.equals(traceability.getTestId())) {
                    traceability.setAbsTime(revTimes3);
                    BigDecimal result = new BigDecimal(30).divide(revTimes3, 3, RoundingMode.HALF_UP);
                    traceability.setAbsSpeed(result);}
                }
                if (text.equals(traceability.getTestId())) {
                    dataList.add(traceability);
                }
            }


        }
        return dataList;
    }

}
